package com.zy.algorithm.controller.data;

import static com.zy.view.SortSquareView.STATUES_SORTED;
import static com.zy.view.SortSquareView.STATUES_SORTING;
import static com.zy.view.SortSquareView.STATUES_UNSORTED;

import android.animation.ValueAnimator;
import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;

import com.zy.algorithm.R;
import com.zy.algorithm.bean.SortStepBean;
import com.zy.algorithm.curtain.IScreenBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DataAndColorController {

    List<TextView> dataTVS = new ArrayList<>();
    private Map<Integer, Integer> colorMap = new ConcurrentHashMap<>();
    private final boolean colorAni;
    private final Resources resources;

    public DataAndColorController(List<TextView> dataTVS, Resources resources, boolean colorAni) {
        this.dataTVS = dataTVS;
        this.resources = resources;
        this.colorAni = false;
    }

    private int getCurStatues(int index) {
        Integer curStatus = colorMap.get(index);
        if (curStatus == null) {
            curStatus = STATUES_UNSORTED;
        }
        return curStatus;
    }

    public void setDataItemRed(int index) {
        int curStatus = getCurStatues(index);
        if (curStatus == STATUES_SORTING || !colorAni) {
            dataTVS.get(index).setBackgroundColor(resources.getColor(R.color.statues_sorting));
        } else {
            //变色动画
            animationColor(dataTVS.get(index), curStatus, STATUES_SORTING);
        }
        colorMap.put(index, STATUES_SORTING);
    }

    public void setDataItemGray(int index) {
        int curStatus = getCurStatues(index);
        if (curStatus == STATUES_UNSORTED || !colorAni) {
            dataTVS.get(index).setBackgroundColor(resources.getColor(R.color.statues_unsorted));
        } else {
            //变色动画
            animationColor(dataTVS.get(index), curStatus, STATUES_UNSORTED);
        }
        colorMap.put(index, STATUES_UNSORTED);
    }

    public void setDataItemGreen(int index) {
        int curStatus = getCurStatues(index);
        if (curStatus == STATUES_SORTED || !colorAni) {
            dataTVS.get(index).setBackgroundColor(resources.getColor(R.color.statues_sorted));
        } else {
            //变色动画
            animationColor(dataTVS.get(index), curStatus, STATUES_SORTED);
        }
        colorMap.put(index, STATUES_SORTED);
    }

    private void animationColor(View view, int curStatus, int newStatues) {
        int cur;
        if (curStatus == STATUES_SORTED) {
            cur = resources.getColor(R.color.statues_sorted);
        } else if (curStatus == STATUES_SORTING) {
            cur = resources.getColor(R.color.statues_sorting);
        } else {
            cur = resources.getColor(R.color.statues_unsorted);
        }

        int to;
        if (newStatues == STATUES_SORTED) {
            to = resources.getColor(R.color.statues_sorted);
        } else if (newStatues == STATUES_SORTING) {
            to = resources.getColor(R.color.statues_sorting);
        } else {
            to = resources.getColor(R.color.statues_unsorted);
        }

        ValueAnimator valueAnimator = ValueAnimator.ofArgb(cur, to);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int color = (int) animation.getAnimatedValue();
                view.setBackgroundColor(color);
            }
        });
        valueAnimator.start();
    }

    public View getViewByIndex(int index) {
        return dataTVS.get(index);
    }

    private void setDataNum(List<Integer> sorted, boolean start, int[] sortStart, int[] sortEnd) {
        //设置数据
        for (int index = 0; index < dataTVS.size(); index++) {
            TextView textView = dataTVS.get(index);
            if (start) {
                int num = sortStart[index];
                if (num == -1) {
                    textView.setText("");
                    textView.setVisibility(View.INVISIBLE);
                } else {
                    textView.setText(Integer.toString(sortStart[index]));
                }
            } else {
                //-1值 - 置为空，不显示
                int num = sortEnd[index];
                if (num == -1) {
                    textView.setText("");
                    textView.setVisibility(View.INVISIBLE);
                } else {
                    textView.setText(Integer.toString(sortEnd[index]));
                }
            }

            if (sorted != null) {
                if (sorted.contains(index)) {
                    setDataItemGreen(index);
                } else {
                    setDataItemGray(index);
                }
            }
        }
    }

    public void setScreenData(IScreenBean bean, boolean start) {
        //设置颜色
        List<Integer> sorted;
        if (start) {
            sorted = bean.getSortedScreenStart();
        } else {
            sorted = bean.getSortedScreenEnd();
        }
        setDataNum(sorted, start, bean.getSortScreenStart(), bean.getSortScreenEnd());
    }

    public void setSortData(SortStepBean bean, boolean start) {
        //设置颜色
        List<Integer> sorted = bean.getStepStartSorted();
        if (start) {
            sorted = bean.getStepStartSorted();
        } else {
            sorted = bean.getStepEndSorted();
        }
        setDataNum(sorted, start, bean.getStepStart(), bean.getStepEnd());
    }
}
